using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using BO;
using Telerik.WinControls.UI;

namespace AnimalFarm.LivestockMedication_manage
{
    public partial class UpdateLivestockMedication : Telerik.WinControls.UI.RadForm
    {
        B_LivestockMedication B_livestockMedication = new B_LivestockMedication();
        B_Vet B_vet = new B_Vet();
        B_Disease B_disease = new B_Disease();
        int quanrantineID,healthID;
        public UpdateLivestockMedication()
        {
            InitializeComponent();
        }

        private void UpdateLivestockMedication_Load(object sender, EventArgs e)
        {
            loadMedicationLivestock();
        }

        //load vet
        private void loadVet(int vetID)
        {
            DataTable aTable = B_vet.GetAllVet();
            drdVet.DataSource = aTable;
            drdVet.DisplayMember = "VetName";
            drdVet.ValueMember = "VetID";
            drdVet.Text = "(Select a vet)";
            drdVet.SelectedValue = vetID;
        }
        //load disease
        private void loadDisease(int diseaseID)
        {
            DataTable aTable = B_disease.GetAllDisease();
            drdDisease.DataSource = aTable;
            drdDisease.DisplayMember = "DiseaseName";
            drdDisease.ValueMember = "DiseaseID";
            drdDisease.Text = "(Select a disease)";
            drdDisease.SelectedValue = diseaseID;
        }

        //load medication livestock
        private void loadMedicationLivestock()
        {
            DataTable aTable = B_livestockMedication.GetAllLivestockMedication();
            DataTable newTable = aTable.Copy();
            newTable.Clear();
            for (int i = 0; i < aTable.Rows.Count; i++)
            {
                bool isTreat = bool.Parse(aTable.Rows[i]["TreatmentStatus"].ToString());
                if (isTreat)
                {
                    newTable.Rows.Add(aTable.Rows[i].ItemArray);
                }
            }
            grvLivestock.DataSource = newTable;
        }

        private void grvLivestock_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvLivestock.SelectedRows.Count > 0)
                {
                    btnAddLivestockMedication.Enabled = true;
                    GridViewRowInfo row = grvLivestock.SelectedRows[0];
                    int vetID = int.Parse(grvLivestock.SelectedRows[0].Cells["VetID"].Value.ToString());
                    loadVet(vetID);
                    int diseaseID = int.Parse(grvLivestock.SelectedRows[0].Cells["DiseaseID"].Value.ToString());
                    loadDisease(diseaseID);
                    healthID = int.Parse(grvLivestock.SelectedRows[0].Cells["HealthID"].Value.ToString());
                    DateTime infectedDate = DateTime.Parse(row.Cells["InfectedDate"].Value.ToString());
                    string symptom = row.Cells["Symptom"].Value.ToString();
                    string treatmentMethod = row.Cells["TreatmentMethod"].Value.ToString();
                    DateTime treatmentDate = DateTime.Parse(row.Cells["TreatmentDate"].Value.ToString());
                    bool isMedication = bool.Parse(row.Cells["TreatmentStatus"].Value.ToString());
                    quanrantineID = int.Parse(row.Cells["QuarantineID"].Value.ToString());

                    dtpInfectedTime.Value = infectedDate;
                    txtSymptom.Text = symptom;
                    txtTreatmentMethod.Text = treatmentMethod;
                    dtpTreatTime.Value = treatmentDate;
                    chkRejoinHerd.Checked = !isMedication;
                }
            }
            catch (Exception ex) { }
        }

        private void btnAddLivestockMedication_Click(object sender, EventArgs e)
        {
            try
            {
                LivestockMedication_Info info = new LivestockMedication_Info();
                info.DiseaseID = int.Parse(drdDisease.SelectedValue.ToString());
                info.HealthID = healthID;
                info.InfectedDate = dtpInfectedTime.Value;
                info.QuarantineID = quanrantineID;
                info.Symptom = txtSymptom.Text;
                info.TreatmentDate = dtpTreatTime.Value;
                info.TreatmentMethod = txtTreatmentMethod.Text;
                info.TreatmentStatus = !chkRejoinHerd.Checked;
                info.VetID = int.Parse(drdVet.SelectedValue.ToString());
                bool isOk = B_livestockMedication.EditOneLivestockMedication(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Livestock medication edited successful", "Livestock medication editing", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadMedicationLivestock();
                    resetField();
                }
                else
                {
                    MessageBox.Show(this, "Livestock medication editing is fail", "Livestock medication editing", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }

        private void resetField()
        {
            if (grvLivestock.Rows.Count == 0)
            {
                btnAddLivestockMedication.Enabled = false;
                drdDisease.DataSource = null;
                drdVet.DataSource = null;
                txtSymptom.Text = "";
                txtTreatmentMethod.Text = "";
                chkRejoinHerd.Checked = false;
            }
            else
            {
                btnAddLivestockMedication.Enabled = true;
                drdDisease.DataSource = null;
                drdVet.DataSource = null;
                txtSymptom.Text = "";
                txtTreatmentMethod.Text = "";
                chkRejoinHerd.Checked = false;
            }
        }
    }
}
